package com.sunteam.flutter_oss_plugin

import com.alibaba.sdk.android.oss.OSS
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult

object FileManager {
    fun uploadFileByPath(oss:OSS?, bucketName: String,
                         objectName: String,
                         filePath: String,
                         resultCallBack: OSSCompletedCallback<PutObjectRequest, PutObjectResult>,
                         progressCallback: OSSProgressCallback<PutObjectRequest>
    ): OSSAsyncTask<PutObjectResult>?{
        val uploadObjectName = if (objectName.isEmpty()) {
            System.currentTimeMillis().toString()
        } else objectName

        val put = PutObjectRequest(bucketName, uploadObjectName, filePath)
        put.callbackParam = mapOf()
        put.progressCallback = progressCallback
        return oss?.asyncPutObject(put, resultCallBack)
    }

    fun getFileOssPath(oss:OSS?,bucketName: String, objectName: String,isSing:Boolean):String{
        if(oss == null) return "";
        if(!isSing) return oss.presignPublicObjectURL(bucketName, objectName)
        if(isSing) return oss.presignConstrainedObjectURL(bucketName, objectName,30 * 60)
        return "";
    }

}